/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MODEL.DAO;

import MODEL.POJO.*;
import MODEL.UTIL.MySqlDataAccessHelper;
import java.sql.ResultSet;

/**
 *
 * @author kijoo
 */
public class HoaDonDAO {

    public static boolean ThemHoaDon(HoaDonPOJO hd) {
        boolean kq = false;
        try {
            String sql = String.format("INSERT INTO HOADON (`HOTENNGUOIMUA`,`DIACHI`,`EMAIL`,`SODIENTHOAI`, `HOTENNGUOINHAN`,`DIACHINGUOINHAN`,`EMAILNGUOINHAN`,`SODIENTHOAINGUOINHAN`) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s')", hd.getHotennguoimua(), hd.getDiachi(), hd.getEmail(), hd.getSodienthoai(), hd.getHotennguoinhan(), hd.getDiachinguoinhan(), hd.getEmailnguoinhan(), hd.getSodienthoainguoinhan());
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.Open();
            int n = helper.executeUpdate(sql);
            if (n == 1) {
                kq = true;
            }
            helper.Close();
        } catch (Exception ex) {
            System.err.println(ex);
        }
        return kq;
    }

    public static boolean XoaHoaDon(int idhoadon) {
        boolean kq = false;
        try {
            String sql = String.format("DELETE FROM HOADON WHERE IDHOADON='%d'",idhoadon);
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.Open();
            int n = helper.executeUpdate(sql);
            if (n == 1) {
                kq = true;
            }
            helper.Close();
        } catch (Exception ex) {
            System.err.println(ex);
        }
        return kq;
    }

    public static int LayMaHoaDonMoiNhat() {
        int mhd=0;
        String sql = String.format("SELECT * FROM HOADON ORDER BY HOADON.IDHOADON DESC");
        try {            
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.executeQuery(sql);
             if (rs.next()) {
                mhd=rs.getInt("IDHOADON");
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return mhd;
    }
}
